# coding: utf-8
import requests,re,pymysql,IPy
def tencentIP():
    r = requests.get(url='http://play.domain.qq.com/getdomain.php')#访问api
    rJosn = r.json()['data']#结果的json转换成字典
    for x in rJosn:#rJosn字典key是域名，value有域名也有ip
        #domainList.append(x)
        updateTenDomain(x)#先把key里的域名传到调度控制域名数据库表
        for y in rJosn[x]:#分开value中的域名和ip
            if re.match("\d+\.\d+\.\d+\.\d+",y):
                exist="不存在"
                for z in DDIP:#ip直接和调度ip进行核对，核对结果上传到调度ip检测的数据库表
                    if y in IPy.IP(z[0]):
                        exist = "已存在："+z[1]
                #IPDict[y]=[x,exist]
                updateIPcheckTab(y,"腾讯",x,exist)#(IP,CP,domain,exist)
            else:
                updateTenDomain(y)#剩余的为域名，上传到调度控制域名数据库表，服务器凌晨自己会检测


def readDB(DBname):
    db = pymysql.connect("113.215.1.161", "root", "root", "mydb", charset="utf8")  # 打开数据库连接
    cursor = db.cursor()  # 使用cursor()方法获取操作游标
    sql = "SELECT * FROM %s WHERE CP='腾讯'" % DBname
    cursor.execute(sql)
    results = cursor.fetchall()
    db.close()
    return results


def updateTenDomain(domian):
    domian=domian.strip().strip("\n")
    db=pymysql.connect("113.215.1.161","root","root","mydb",charset="utf8" )# 打开数据库连接
    cursor = db.cursor()# 使用cursor()方法获取操作游标
    #查询
    sqlFind='SELECT * FROM DDIP_Domain_tencent WHERE Domain="%s"' % (domian,)
    #更新
    sqlupdate='UPDATE DDIP_Domain_tencent SET CP="腾讯" WHERE Domain="%s" ' % (domian,)
    #插入
    sqlInsert='INSERT INTO DDIP_Domain_tencent(CP,Domain) VALUES ("腾讯","%s")' % (domian,)
    print(sqlFind)
    Find=cursor.execute(sqlFind)
    print("找到%s条记录" % Find)
    if Find==0:#如果库中无次记录
        try:
            cursor.execute(sqlInsert)  # 插入记录
            print(sqlInsert,"成功")
        except:
            print(sqlInsert,"失败")
            db.close()
            return 0
    else:
        try:
            cursor.execute(sqlupdate)#更新记录
            print(sqlupdate,"成功")
        except:
            print(sqlupdate, "失败")
            db.close()
            return 0
    db.close()

def updateIPcheckTab(IP,CP,domain,exist):
    db=pymysql.connect("113.215.1.161","root","root","mydb",charset="utf8" )# 打开数据库连接
    cursor = db.cursor()# 使用cursor()方法获取操作游标
    #查询
    sqlFind='SELECT * FROM IPcheckTab WHERE IP="%s"' % IP
    #更新
    sqlupdate='UPDATE IPcheckTab SET CP="%s",domain="%s",exist="%s" WHERE IP="%s" ' % (CP,domain,exist,IP)
    #插入
    sqlInsert='INSERT INTO IPcheckTab(IP,CP,domain,exist) VALUES ("%s","%s","%s","%s")' % (IP,CP,domain,exist)
    print(sqlFind)
    Find=cursor.execute(sqlFind)
    print("找到%s条记录" % Find)
    if Find==0:#如果库中无次记录
        try:
            cursor.execute(sqlInsert)  # 插入记录
            print(sqlInsert,"插入成功")
        except:
            print(sqlInsert,"插入失败")
            db.close()
            return 0
    else:
        try:
            cursor.execute(sqlupdate)#更新记录
            print(sqlupdate,"更新成功")
        except:
            print(sqlupdate, "更新失败")
            db.close()
            return 0
    db.close()



if __name__ == '__main__':
    DDIP = readDB("DDIPtab")#获取调度ip信息
    print(DDIP)
    tencentIP()#访问腾讯调度api





